package com.digipepper.GPStest;

/*
http://d.hatena.ne.jp/yamashina/20090613/1244901811
*/

import java.util.List;
import java.util.ArrayList;
//import java.util.Random;

import _root_.android.util.{DisplayMetrics,Log};
import _root_.android.os.{Handler,Bundle};
import _root_.android.app.Activity;
import _root_.android.content.Context;
import _root_.android.view.{MotionEvent,View};
import _root_.android.view.{Window,WindowManager};
import _root_.android.view.{Menu,MenuItem,MenuInflater};
import _root_.android.graphics.{Canvas,Color,Paint};
import _root_.android.graphics.Paint.Style;
//import _root_.android.hardware.{Sensor,SensorEvent,SensorEventListener,SensorManager};

import _root_.android.location.Location;
import _root_.android.location.Criteria;
import _root_.android.location.LocationListener;
import _root_.android.location.LocationManager;
import _root_.android.location.LocationProvider;

import scala.util.Random;

// --
class mainView extends Activity with LocationListener {

  val mView:MyView = new MyView( getApplication() );
	var mLocationManager:LocationManager = null;

	override def onCreate(savedInstanceState:Bundle) {

    super.onCreate(savedInstanceState);
//		super.onCreate(null);

    mLocationManager = getSystemService(Context.LOCATION_SERVICE).asInstanceOf[LocationManager];

    setContentView(mView);

	}

  override def onPause() {
        if (mLocationManager != null) {
            mLocationManager.removeUpdates(this);
        }
            super.onPause();
  }

    override def onResume()
    {
        if (mLocationManager != null) {
            mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
//                LocationManager.NETWORK_PROVIDER,
                0,
                0,
                this);
        }

      super.onResume();
    }
   
    override def onStop()
    {
      super.onStop();
      System.exit(0);
    }

	override def onLocationChanged(location:Location) {
		mView.setLocation(location);
		mView.invalidate();
	}

	// 終了メニューを作成1
	override def onCreateOptionsMenu(menu:Menu):Boolean = {
		var inflater:MenuInflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);

		true;
	}

	// 終了メニューを作成2
	override def onMenuItemSelected(featureId:Int, item:MenuItem):Boolean = {
		var id:Int = item.getItemId();

		//XML中のメニューボタンにアクセスするにはR.id以下を利用する
		if (id == R.id.End) {
/*
・Log.e()：エラー
・Log.w()：警告
・Log.i()：情報
・Log.d()：デバッグ
・Log.v()：冗長
*/
			//TODO 再検索
			Log.d("Menu", "itemId: 0");
			finish();
		}

		true;
	}
	override def onProviderDisabled(provider:String) {

    }

	override def onProviderEnabled(provider:String) {

    }

	override def onStatusChanged(provider:String, status:Int, extras:Bundle) {
  }

}

class MyView(context:Context) extends View(context) {

	var objLoc:Location = null;

		val TEXT_SIZE = 16;

		var paint:Paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(TEXT_SIZE);
		paint.setColor(0xFFFFFFFF);

	//super(context);
	setFocusable(true);

	override def onDraw(canvas:Canvas) {
		if(objLoc != null){
			canvas.drawText("Lat = ("+objLoc.getLatitude().toString+")", 0, TEXT_SIZE*1, paint);
			canvas.drawText("Long = ("+objLoc.getLongitude().toString+")", 0, TEXT_SIZE*2, paint);
			canvas.drawText("Accuracy = ("+objLoc.getAccuracy().toString+")", 0, TEXT_SIZE*3, paint);
			canvas.drawText("Altitude = ("+objLoc.getAltitude().toString+")", 0, TEXT_SIZE*4, paint);
			canvas.drawText("Time = ("+objLoc.getTime().toString+")", 0, TEXT_SIZE*5, paint);
			canvas.drawText("Speed = ("+objLoc.getSpeed().toString+")", 0, TEXT_SIZE*6, paint);
			canvas.drawText("Bearing = ("+objLoc.getBearing().toString+")", 0, TEXT_SIZE*7, paint);

		}
  }

	def setLocation(location:Location){
		objLoc = location;
		Log.v("----------", "----------");
		Log.v("Latitude", location.getLatitude().toString);
		Log.v("Longitude", location.getLongitude().toString);
		Log.v("Accuracy", location.getAccuracy().toString);
		Log.v("Altitude", location.getAltitude().toString);
		Log.v("Time", location.getTime().toString);
		Log.v("Speed", location.getSpeed().toString);
		Log.v("Bearing", location.getBearing().toString);
	}
}